home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicSpinnerUI.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  236 lines

  1. /*
  2.  * @(#)OrganicSpinnerUI.java    1.5 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.border.*;
  27. import com.sun.java.swing.plaf.*;
  28.  
  29. import java.io.Serializable;
  30.  
  31. import com.sun.java.swing.plaf.basic.BasicSpinnerUI;
  32. import com.sun.java.swing.plaf.basic.StringSpinner;
  33. import com.sun.java.swing.plaf.basic.Spinner;
  34.  
  35.  
  36.  
  37. /**
  38.  * OrganicSpinnerUI implementation
  39.  * <p>
  40.  * Warning: serialized objects of this class will not be compatible with
  41.  * future swing releases.  The current serialization support is appropriate
  42.  * for short term storage or RMI between Swing1.0 applications.  It will
  43.  * not be possible to load serialized Swing1.0 objects with future releases
  44.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  45.  * baseline for the serialized form of Swing objects.
  46.  *
  47.  * @version 1.5 02/02/98
  48.  * @author Steve Wilson
  49.  */
  50. public class OrganicSpinnerUI extends BasicSpinnerUI
  51. {
  52.  
  53.   // Shared UI object
  54.   protected static OrganicSpinnerUI organicSpinnerUI = new OrganicSpinnerUI();
  55.  
  56.   private static Border organicInactiveBorder = new EmptyBorder(0,0,0,0);
  57.   private static Point buttonSize = new Point( 17, 14);
  58.  
  59.   public static ComponentUI createUI(JComponent s)  {
  60.     return organicSpinnerUI;
  61.   }
  62.   
  63.   public void installUI(JComponent c) {
  64.     Spinner spinner = (Spinner)c;
  65.     installBorder(c);
  66.  
  67.     super.installUI(c);
  68.     c.setBackground(OrganicLookAndFeel.getControl1());
  69.     JButton up = new OrganicSpinnerButton(new OrganicArrowIcon(true));
  70.     up.addActionListener( new SpinnerIncrementer(spinner));
  71.  
  72.     JButton down = new OrganicSpinnerButton(new OrganicArrowIcon(false));
  73.     down.addActionListener( new SpinnerDecrementer(spinner));
  74.  
  75.     Dimension size = getPreferredSize(c);
  76.     up.setBounds ( size.width-buttonSize.x, 0, buttonSize.x, buttonSize.y);
  77.     down.setBounds (  size.width-buttonSize.x, buttonSize.y-2, buttonSize.x,buttonSize.y);
  78.     c.setLayout(null);
  79.  
  80.     c.add(up);
  81.     c.add(down);
  82.   }
  83.   
  84.   public void uninstallUI(JComponent c) {
  85.     Border b = c.getBorder();
  86.     if(b == organicInactiveBorder)
  87.       c.setBorder(null);
  88.  
  89.     Component[] children = c.getComponents();
  90.     for (int i = 0; i < children.length; i++) {
  91.       if (children[i] instanceof OrganicSpinnerButton ){
  92.         c.remove(children[i]);
  93.       }
  94.     }
  95.     super.uninstallUI(c);
  96.   }
  97.     
  98.   public void paint(Graphics g, JComponent c) {
  99.     Spinner spinner = ((Spinner)(c));
  100.     Insets bi = spinner.getBorder().getBorderInsets(spinner);
  101.  
  102.     Dimension size = c.getSize();
  103.     Color back = spinner.getBackground();
  104.     g.setColor(back);
  105.     g.fillRect(bi.left,bi.top,size.width-(bi.left+bi.right),size.height-(bi.top+bi.bottom));   
  106.  
  107.  
  108.     final int offset = 4;
  109.     Dimension spinnerArea = getMinimumSize(c);
  110.     bi.top += offset;
  111.     spinnerArea.height += offset;
  112.     paintSpinnerArea(g, spinner, spinnerArea, bi);
  113.  
  114.     int entryWidth = spinnerArea.width;
  115.     int entryHeight = spinnerArea.height + bi.top;
  116.     int bottomPad =  offset;
  117.  
  118.     g.setColor(OrganicLookAndFeel.getControl3());
  119.     g.fillRect( 1, entryHeight - (bottomPad+1), 2, 2);
  120.     g.fillRect( entryWidth-3, entryHeight - (bottomPad+1), 2, 2);
  121.     
  122.     for (int xDot = 5; xDot < entryWidth-3; xDot+=2) {
  123.         g.fillRect( xDot, entryHeight-bottomPad, 1,1);
  124.     }
  125.   }
  126.  
  127.   protected void paintSpinnerArea(Graphics g, Spinner spinner, Dimension d, Insets bi) {
  128.  
  129.     if (spinner.hasFocus()) {
  130.       g.setColor(getFocusColor());
  131.       g.fillRect(bi.left+2, bi.top+2, d.width-(bi.left+bi.right+4), d.height-(bi.top+bi.bottom+4));
  132.       g.setColor(Color.white);
  133.     }
  134.     else {
  135.       Color forG = spinner.getForeground(); 
  136.       g.setColor(forG);
  137.     }
  138.     String s;
  139.     if(spinner instanceof StringSpinner) {
  140.       StringSpinner spinner2 = ((StringSpinner)(spinner));
  141.       s = spinner2.getValueName() + (spinner2.getText()!=null ? spinner2.getText() : "");
  142.       if (spinner2.getTypedString()==null) {
  143.     g.drawString(s, bi.left+2, ascent + bi.top+2);
  144.       }
  145.       else  {
  146.     g.setColor(Color.lightGray);
  147.     g.drawString(s, bi.left+2, ascent+bi.top+2);
  148.     g.setColor(Color.white);
  149.     g.drawString(s.substring(0,spinner2.getTypedString().length()),bi.left+2 , ascent+bi.top+2);
  150.       }
  151.     }
  152.     else {
  153.       s = Integer.toString(spinner.getValue());
  154.       if(spinner.getLeadingPad() != -1){
  155.     int j = spinner.getDigits()-s.length();
  156.     String lp = Integer.toString(spinner.getLeadingPad());
  157.     for(int i=0; i<j; i++) { s = lp +s;}
  158.       }
  159.       s += (spinner.getText()!=null ? spinner.getText() : "");
  160.       g.drawString(s, d.width-fm.stringWidth(s) - bi.right-2, ascent + bi.top+2); // fixed bug
  161.     }
  162.   }
  163.  
  164.   protected void installBorder (JComponent c) {
  165.    Border b = c.getBorder();
  166.     if( b == null)
  167.       c.setBorder(organicInactiveBorder);
  168.   }
  169.  
  170.   public Dimension getPreferredSize(JComponent c) {
  171.     Dimension size = getMinimumSize(c);
  172.     size.width += 20;
  173.     size.height +=10;
  174.     return size;
  175.   }
  176.   protected Color getFocusColor() {
  177.       return OrganicLookAndFeel.getHighlight4();
  178.   }
  179.  
  180.  
  181. }
  182.  
  183.  
  184. class OrganicSpinnerButton extends JAutoRepeatButton {
  185.  
  186.  
  187.   public OrganicSpinnerButton (Icon icon) {
  188.     super(icon);
  189.   }
  190.  
  191.   public boolean isFocusTraversable() {  // here to prevent focus aquisition
  192.     return false;
  193.   }
  194.   public void requestFocus() {} // stubbed to prevent focus aquisition
  195. }
  196.  
  197.  
  198. abstract class SpinnerAdaptor implements ActionListener {
  199.  
  200.   protected Spinner spinner;
  201.  
  202.   public SpinnerAdaptor(Spinner s) {
  203.     spinner = s;
  204.   }
  205.  
  206. }
  207.  
  208. class SpinnerIncrementer extends SpinnerAdaptor {
  209.  
  210.  
  211.   public SpinnerIncrementer(Spinner s) {
  212.     super(s);
  213.   }
  214.  
  215.   public void actionPerformed(ActionEvent e) {
  216.       int value = spinner.getValue();
  217.       int inc = spinner.getUnitIncrement();
  218.       value += inc;
  219.       spinner.setValue(value);
  220.   }
  221. }
  222.  
  223. class SpinnerDecrementer extends SpinnerAdaptor {
  224.  
  225.   public SpinnerDecrementer(Spinner s) {
  226.     super(s);
  227.   }
  228.  
  229.   public void actionPerformed(ActionEvent e) {
  230.      int value = spinner.getValue();
  231.       int inc = spinner.getUnitIncrement();
  232.       value -= inc;
  233.       spinner.setValue(value);
  234.   }
  235. }
  236.